// pages/print/baiduCloud/baiduCloud.js
var utils = require("../../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picFixShow: false,
    list: [],
    firstOpen: 0,
    lastPath: '',
    lastName: '',
    key:''
  },
  backNav() {
    wx.navigateBack()
  },
  clpi() {
    wx.setClipboardData({
      data: '百度网盘授权',
      success(res) {
        wx.getClipboardData({
          success(res) {
          }
        })
      }
    })
  },
  //获取下级目录
  getNextFir(e) {
    var that = this;
    var dir = e.currentTarget.dataset.dir;
    var name = e.currentTarget.dataset.name;
    wx.showLoading()
    utils.request('member/baiduDisk', {
      dir: dir
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 1) {
        if (res.data.data.isNeedAuth == 1) {
          that.setData({
            picFixShow: true,
            lastPath:''
          })
        } else {
          var lastPath = dir.replace(name, "")
          if (lastPath.substring(0, 1) == '/' && lastPath.length > 1) {
            lastPath = lastPath.substring(0, lastPath.length - 1);
          } else {
            lastPath = '/' + lastPath.substring(0, lastPath.length - 1);
          }
          that.setData({
            list: res.data.data,
            lastPath: lastPath,
            lastName: name
          })
        }
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  addKey(e){
    this.setData({
      key:e.detail.value
    })
  },
  searchVal(){
    var that = this;
    wx.showLoading()
    utils.request('member/baiduDisk', {
      keyword:that.data.key
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 1) {
        if (res.data.data.isNeedAuth == 1) {
          that.setData({
            picFixShow: true,
            lastPath:''
          })
        } else {
          that.setData({
            list: res.data.data
          })
        }
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  //搜索文件

  //获取用户文件列表
  getIndex() {
    var that = this;
    wx.showLoading()
    utils.request('member/baiduDisk', {}, function (res) {
      wx.hideLoading()
      if (res.data.code == 1) {
        if (res.data.data.isNeedAuth == 1) {
          that.setData({
            picFixShow: true,
            lastPath:''
          })
        } else {
          that.setData({
            picFixShow: false,
            list: res.data.data
          })
        }
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  //设置个空白方法，阻止冒泡
  touchAnother() {

  },
  //点击文件获取链接下载
  downFile(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showLoading()
    utils.request('member/baiduDisk', {
      id: id,
      sn:wx.getStorageSync('machineQrCode')
    }, function (res) {
      if (res.data.code == 1) {
        wx.hideLoading()
        if (res.data.data.isNeedAuth == 1) {
          that.setData({
            picFixShow: true,
            lastPath:''
          })
        }else{
          wx.redirectTo({
              url: '../list/list?code=29682339&name=' + res.data.data.filename + '&icon=' + res.data.data.fileIcon + '&path=' + res.data.data.fileUrl + '&pageNum=' + res.data.data.pageNum
          })
        }
      } else {
        wx.hideLoading()
        utils.toast(res.data.msg)
      }
    })
  },
  // 返回上一层
  goFirst() {
    var that = this;
    var dir = that.data.lastPath;
    wx.showLoading()
    utils.request('member/baiduDisk', {
      dir: dir
    }, function (res) {
      wx.hideLoading()
      if (res.data.code == 1) {
        if (res.data.data.isNeedAuth == 1) {
          that.setData({
            picFixShow: true,
            lastPath:''
          })
        }else{
          if (dir == '/') {
            dir = ''
          } else {
            var newStr = dir.split("/")
            dir = dir.replace('/' + newStr[newStr.length - 1], "")
            if (dir == '') {
              dir = '/'
            }
          }
          that.setData({
            picFixShow: false,
            list: res.data.data,
            lastPath: dir
          })
        }
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  //文档处理
  settingDoc(path,name,icon,pageNum) {
    // utils.uploadfile(path, name, function (res) {
    //   var data = JSON.parse(res.data)
    //   wx.hideLoading()
    //   if (data.code == 1) {
    //     wx.redirectTo({
    //       url: '../list/list?code=' + code + '&name=' + data.name + '&icon=' + data.fileIcon + '&path=' + data.url + '&pageNum=' + data.pageNum,
    //     })
    //   } else {
    //     utils.toast(res.data.msg);
    //   }
    // })
  },
  hidePicFix() {
    this.setData({
      picFixShow: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIndex();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.firstOpen == 1) {
      this.getIndex();
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})